package com.yx.learn.service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yx.learn.dao.PositionDao;
import com.yx.learn.domain.Position;
import com.yx.learn.service.PositionService;

@Service("positionService")
@Transactional
public class PositionServiceImpl implements PositionService {
	
	@Autowired
	@Qualifier("positionDao")
	private PositionDao positionDao;

	public void savePosition(Position position) throws Exception{
		// TODO Auto-generated method stub
		this.positionDao.save(position);
	}

	public void deletePosition(Position position) throws Exception{
		// TODO Auto-generated method stub
		this.positionDao.delete(position);
	}

	public void updatePositon(Position position) throws Exception{
		// TODO Auto-generated method stub

		this.positionDao.update(position);
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public String findAllToJson() throws Exception{
		// TODO Auto-generated method stub

		List<Position> positions = this.positionDao.findAll();
		int total =  positions.size();
		JSONArray ja = new JSONArray();
		for(int i=0;i<total;i++){
			JSONObject jo =  new JSONObject();
			jo.put("id", positions.get(i).getId());
			jo.put("positionCode", positions.get(i).getPositionCode());
			jo.put("positionName", positions.get(i).getPositionName());
			ja.add(jo);
		}
		String json = ja.toString();
		json = "{\"total\":" + total + ",\"rows\":" + json + "}";
		
		return json;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public String findPositions(int pageNumber, int pageSize) throws Exception{
		// TODO Auto-generated method stub

		String hql = "From Position";
		List<Position> positions = positionDao.findPositions(hql, pageNumber, pageSize);
		int total = positionDao.findAll().size();
		JSONArray ja = new JSONArray();
		for(int i=0;i<positions.size();i++){
			JSONObject jo =  new JSONObject();
			jo.put("id", positions.get(i).getId());
			jo.put("positionCode", positions.get(i).getPositionCode());
			jo.put("positionName", positions.get(i).getPositionName());
			ja.add(jo);
		}
		String json = ja.toString();
		json = "{\"total\":" + total + ",\"rows\":" + json + "}";
		
		return json;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public String positionsToCombobox() throws Exception{
		// TODO Auto-generated method stub

		List<Position> positions = this.positionDao.findAll();
		JSONArray ja = new JSONArray();
		for(int i=0;i<positions.size();i++){
			JSONObject jo = new JSONObject();
			jo.put("id", positions.get(i).getId());
			jo.put("positionName", positions.get(i).getPositionName());
			ja.add(jo);
		}
		String json = ja.toString();
		return json;
	}

	public Position findById(int id) throws Exception {
		// TODO Auto-generated method stub

		Position position = positionDao.findById(id);
		return position;
	}
	
}

